
#include "ComponentNode.h"
#include <utility>
#include <algorithm>
#include <iostream>

using namespace npComposite;
using namespace std;


ComponentNode::ComponentNode() : Component() // class inheritance
{
    //To initialize data member;
    this->components = vector<Component*>();
}

ComponentNode::~ComponentNode()
{
    //To delete dynamic data member
    for (size_t i = 0; i < this->components.size(); i++)
    {
        delete this->components[i];
    }
    
    cout << "destroyed************************" << endl;
}

ComponentNode::ComponentNode(const ComponentNode& cpy) : Component(cpy) // class inheritance
{
    //To copy data member
    this->components = cpy.components;
}

ComponentNode& ComponentNode::operator=(const ComponentNode& asn)
{
    if (this == &asn)
    {
        return *this;
    }
    else
    {
        //To invoke base class operator=
        Component::operator=(asn);
        //To assign data member, note to delete dynamic member if exists.
        for (size_t i = 0; i < this->components.size(); i++)
        {
            delete this->components[i];
        }
        
        this->components = asn.components;
        return *this;
    }
}

ComponentNode* ComponentNode::clone() const
{
    //return new
    return new ComponentNode(*this);
}

//other constructors
ComponentNode::ComponentNode(string name) : Component(name)
{
    this->components = vector<Component*>();
}

//public methods
void ComponentNode::add(Component* component)
{
    this->components.push_back(component);
}

void ComponentNode::remove(Component* component)
{
    auto it = std::remove(this->components.begin(), this->components.end(), component);
    delete *it;
}

void ComponentNode::display(int depth)
{
    for (size_t i = 0; i < depth; i++)
    {
        cout << "-";
    }
    cout << endl;

    for (auto i = this->components.begin(); i != this->components.end(); i++)
    {
        (*i)->display(depth + 2);
    }
}

//protected methods

//private methods
